/**
 * Copyright (c) 2015-2016 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
 * EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
 * and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
 */

package com.langsheng.lsintell.ar;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.alibaba.fastjson.JSONObject;
import com.langsheng.lsintell.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import cn.easyar.engine.EasyAR;

public class TestARActivity extends AppCompatActivity {


    private final static String IMAGE_1 = "argame00.jpg";
    private final static String IMAGE_2 = "idback.jpg";
    private final static String IMAGE_3 = "namecard.jpg";
    private final static String VIDEO_1 = "namecard.mp4";
    private final static String VIDEO_2 = "transparentvideo.mp4";
    private final static String VIDEO_3 = "video.mp4";

    private final static String JSON_NAME = "targets.json";

    private String FILE_ROOT = "";
    private LSAr ar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_ar);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        FILE_ROOT = getExternalFilesDir(null) + "/";
        ar = new LSAr();

        copyTestFiles();
        createTargetJson();
        EasyAR.initialize(this, LSAr.key);
        ar.nativeInit(FILE_ROOT + JSON_NAME);

        GLView glView = new GLView(this);
        glView.setRenderer(new Renderer());
        glView.setZOrderMediaOverlay(true);

        ((ViewGroup) findViewById(R.id.preview)).addView(glView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ar.nativeRotationChange(getWindowManager().getDefaultDisplay().getRotation() == android.view.Surface.ROTATION_0);
    }

    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        ar.nativeRotationChange(getWindowManager().getDefaultDisplay().getRotation() == android.view.Surface.ROTATION_0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ar.nativeDestory();
    }

    @Override
    protected void onResume() {
        super.onResume();
        EasyAR.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        EasyAR.onPause();
    }

    public void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1024];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();

        }
    }

    private void copyMyFile(String fileName) {
        String Oldpath = Environment.getExternalStorageDirectory() + "/" + fileName;
        String newPath = getExternalFilesDir(null) + "/" + fileName;
        File file = new File(newPath);
        if (!file.exists()) {
            copyFile(Oldpath, newPath);
        }
    }

    public void copyTestFiles() {
        copyMyFile(IMAGE_1);
        copyMyFile(IMAGE_2);
        copyMyFile(IMAGE_3);
        copyMyFile(VIDEO_1);
        copyMyFile(VIDEO_2);
        copyMyFile(VIDEO_3);
    }

    private void createTargetJson() {
        List<TargetJson.ImagesBean> beanList = new ArrayList<>();
        TargetJson.ImagesBean bean = new TargetJson.ImagesBean();
        bean.setImage(FILE_ROOT + IMAGE_1);
        bean.setName(FILE_ROOT + VIDEO_1);
        beanList.add(bean);
        bean = new TargetJson.ImagesBean();
        bean.setImage(FILE_ROOT + IMAGE_2);
        bean.setName(FILE_ROOT + VIDEO_2);
        beanList.add(bean);
        bean = new TargetJson.ImagesBean();
        bean.setImage(FILE_ROOT + IMAGE_3);
        bean.setName(FILE_ROOT + VIDEO_3);
        beanList.add(bean);

        TargetJson json = new TargetJson();
        json.setImages(beanList);

        String jsonPath = FILE_ROOT + JSON_NAME;
        File file = new File(jsonPath);
        try {
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.print(JSONObject.toJSON(json).toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
